package com.fz.us.oa.web.controller.main.menu;

import com.fz.us.admin.base.utils.LogUtil;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.oa.core.entity.com.ModulePower;
import com.fz.us.oa.core.service.com.ModulePowerService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;

/**
 * 控制器 - 菜单
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-01 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/menu")
public class MenuController extends BaseController {

    private static final long serialVersionUID = 1971143987753063055L;

    @Autowired
    private ModulePowerService modulePowerService;

    /**
     * 菜单路由(一层菜单)
     */
    @RequestMapping(value = "{menu}", method = RequestMethod.GET)
    public String menu(Model model, @PathVariable String menu,  String type) {
        model.addAttribute("type",type);
        return "/main/menu/"+menu;
    }

    /**
     * 菜单路由(二层菜单)
     */
    @RequestMapping(value = "{menu1}/{menu2}", method = RequestMethod.GET)
    public String menu(@CurrentUser User user, Model model,String viewtype,@PathVariable String menu1, @PathVariable String menu2) {
        // 任务视图的特殊处理
        if (menu1.equals("admin") && menu2.equals("task")) {
            ArrayList<DictItem> dictItems = getDictItems(user,"taskPrj");
            model.addAttribute("dictItems",dictItems);
        }
        String isCreate = isCreate("com.fz.us.oa.core.entity." + menu1 + "." + menu2.substring(0, 1).toUpperCase() + menu2.substring(1));
        model.addAttribute("isCreate",isCreate);
        model.addAttribute("viewtype",viewtype);

        ModulePower modulePower=modulePowerService.getModulePowerPassByUser(user);
        String entityName="com.fz.us.oa.core.entity."+ menu1 + "."+ menu2.substring(0, 1).toUpperCase() +  menu2.substring(1);
        model.addAttribute("isStart", "style='display:none'");
        if(modulePower!=null){
            String entityNames=modulePower.getEntityName();
            if(entityNames.contains(entityName)) {
                model.addAttribute("isStart", "");
            }
        }

        return "/main/menu/"+menu1+"/"+menu2;
    }

}
